<template>
    <div>
        <el-form :inline="true">
            <el-form-item>
                <el-input
                        v-model="searchForm.name"
                        placeholder="姓名"
                >
                </el-input>
            </el-form-item>

            <el-form-item>
                <el-input
                        v-model="searchForm.phone"
                        placeholder="手机号"
                >
                </el-input>
            </el-form-item>

            <el-form-item>
                <el-button @click="getWarnList()">搜索</el-button>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="dialogVisible = true">新增</el-button>
            </el-form-item>
            <!--            <el-form-item>-->
            <!--                <el-popconfirm title="这是确定批量删除吗？" @confirm="delHandle(null)">-->
            <!--                    <el-button type="danger" slot="reference" :disabled="delBtlStatu">批量删除</el-button>-->
            <!--                </el-popconfirm>-->
            <!--            </el-form-item>-->
        </el-form>

        <el-table
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                border
                stripe
                @selection-change="handleSelectionChange">

            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>

            <el-table-column
                    prop="warnName"
                    label="告警人姓名">
            </el-table-column>
            <el-table-column
                    prop="warnPhone"
                    label="告警人手机号">
            </el-table-column>

            <el-table-column
                    prop="warnContent"
                    label="告警内容">
            </el-table-column>

            <el-table-column
                    prop="warnState"
                    label="状态">
                <template slot-scope="scope">
                    <el-tag size="small" v-if="scope.row.warnState === 1" type="success">正常</el-tag>
                    <el-tag size="small" v-else-if="scope.row.warnState === 0" type="danger">禁用</el-tag>
                </template>

            </el-table-column>
            <el-table-column
                    prop="icon"
                    label="操作">

                <template slot-scope="scope">

                    <el-button type="text" @click="editHandle(scope.row.id)">编辑</el-button>
                    <el-divider direction="vertical"></el-divider>

                    <el-button type="text" @click="enableHandle(scope.row.id)">启用</el-button>
                    <el-divider direction="vertical"></el-divider>


                    <template>
                        <el-popconfirm title="确定要禁用吗？" @confirm="disableHandle(scope.row.id)">
                            <el-button type="text" slot="reference">禁用</el-button>
                        </el-popconfirm>
                    </template>
                    <el-divider direction="vertical"></el-divider>

                    <template>
                        <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)">
                            <el-button type="text" slot="reference">删除</el-button>
                        </el-popconfirm>
                    </template>


                </template>
            </el-table-column>

        </el-table>


        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                layout="total, sizes, prev, pager, next, jumper"
                :page-sizes="[10, 20, 50, 100]"
                :current-page="searchForm.current"
                :page-size="searchForm.size"
                :total="total">
        </el-pagination>


        <!--新增对话框-->
        <el-dialog
                :title="title"
                :visible.sync="dialogVisible"
                width="600px"
                :before-close="handleClose">

            <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">

                <el-form-item label="告警人姓名" prop="warnName" label-width="100px">
                    <el-input v-model="editForm.warnName" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="手机号" prop="warnPhone" label-width="100px">
                    <el-input v-model="editForm.warnPhone" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="告警内容" prop="warnContent" label-width="100px">
                    <el-input
                            type="textarea"
                            :rows="4"
                            placeholder="请输入内容"
                            v-model="editForm.warnContent">
                    </el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('editForm')">提交</el-button>
                    <el-button @click="resetForm('editForm')">取消</el-button>
                </el-form-item>
            </el-form>

        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "Warn",
        data() {
            return {
                title: '新增告警信息',
                searchForm: {
                    size: 10,
                    current: 1
                },
                total: 0,
                tableData: [],
                dialogVisible: false,
                editForm: {},
                editFormRules: {
                    warnName: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ],
                    warnPhone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'}
                    ],
                    warnContent: [
                        {required: true, message: '请输入告警内容', trigger: 'blur'}
                    ]
                },
                multipleSelection: []
            }
        },
        created() {
            this.getWarnList()
        },
        methods: {
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.searchForm.size = val
                this.getWarnList()
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.searchForm.current = val
                this.getWarnList()
            },
            handleSelectionChange(val) {
                console.log("勾选")
                console.log(val)
                this.multipleSelection = val;
            },
            getWarnList() {
                this.$axios.post("/warn/list", this.searchForm)
                    .then(res => {
                        this.tableData = res.data.data.records
                        this.size = res.data.data.size
                        this.current = res.data.data.current
                        this.total = res.data.data.total
                    })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.dialogVisible = false
                this.editForm = {}
            },
            handleClose() {
                this.resetForm('editForm')
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post('/warn/' + (this.editForm.id ? 'update' : 'save'), this.editForm)
                            .then(res => {

                                this.$message({
                                    showClose: true,
                                    message: '恭喜你，操作成功',
                                    type: 'success',
                                    onClose: () => {
                                        this.getWarnList()
                                    }
                                });

                                this.dialogVisible = false
                                this.resetForm(formName)
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            editHandle(id) {
                this.$axios.get('/warn/info/' + id).then(res => {
                    this.editForm = res.data.data

                    this.title = "编辑告警信息";
                    this.dialogVisible = true
                })
            },
            enableHandle(id){
                this.$axios.post('/warn/enable/'+id)
                .then(res => {
                    this.$message({
                        showClose: true,
                        message: '恭喜你，操作成功',
                        type: 'success',
                        onClose: () => {
                            this.getWarnList()
                        }
                    });
                })
            },
            disableHandle(id){
                this.$axios.post('/warn/disable/'+id)
                .then(res => {
                    this.$message({
                        showClose: true,
                        message: '恭喜你，操作成功',
                        type: 'success',
                        onClose: () => {
                            this.getWarnList()
                        }
                    });
                })
            },
            delHandle(id){
                var ids = []

                if (id) {
                    ids.push(id)
                } else {
                    this.multipleSelection.forEach(row => {
                        ids.push(row.id)
                    })
                }
                console.log(ids)
                this.$axios.post("/warn/delete", ids).then(res => {
                    this.$message({
                        showClose: true,
                        message: '恭喜你，操作成功',
                        type: 'success',
                        onClose:() => {
                            this.getWarnList()
                        }
                    });
                })
            }
        }
    }
</script>

<style scoped>
    .el-pagination {
        float: right;
        margin-top: 22px;
    }
</style>